// pages/printing/index.js
import {
  request
} from '../../api/index'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    printingMethodId: '',
    /**印花 */
    show: false,
    columnsValue: {},
    recordState:  app.globalData.editState,
    columns: [{
        text: '喷墨打印',
        value: 0
      },
      {
        text: '丝网打印',
        value: 1
      }
    ],
  },

  /**
   * 设置页面属性
   */
  setPageProperty(options) {
    wx.setNavigationBarTitle({
      title: '印花方式'
    })
    this.setData({
      id: options.id,
      fromNavigation: options.navigation || 0,
      recordState: app.globalData.editState
    })
  },
  /**
   * 查询印花列表
   */
  fetchData() {
    request.printingMethodList({
      collectionRecordIds: this.data.id
    }, (res) => {
      this.setData({
        columnsValue: this.data.columns.find(item => item.value === res[0]?.printingMethod),
        printingMethodId: res[0]?.id
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setPageProperty(options)
    this.fetchData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 印花
   */
  showPicker() {
    this.setData({
      show: true
    })
  },
  pickerChangeHandle({
    detail
  }) {
    const app = getApp();
    if(!app.globalData.editState) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '当前记录状态不能修改'
      })
      return
    }
    request.printingMethodAdd({
      printingMethod: detail.value.value,
      collectionRecordId: this.data.id,
      id: this.data.printingMethodId
    }, (res) => {
      this.setData({
        columnsValue: this.data.columns.find(item => item.value === detail.value.value)
      })
      this.onClose();
      this.next();
    })
  },
  /**印花 */
  onClose() {
    this.setData({
      show: false
    })
  },
  next() {
    if (this.data.fromNavigation > 0) {
      wx.navigateBack({
        delta: 1
      })
      return
    }
    wx.redirectTo({
      url: `../glaze/index?id=${_this.data.id}&next=firing&sortNumber=1&pageTitle=胚体釉料2` 
    })
  },
  tohome() {
    wx.navigateBack(8)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.onAppRoute((res) => {
      if (res.path === 'pages/printing/index' && res.openType === 'navigateBack') {
        this.fetchData();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})